﻿using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Xml.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;

namespace Klinkby.MicroBus
{
    /// <summary>
    /// Immediately publishes a message to a WCF endpoint
    /// </summary>
    [Serializable]
    internal class Sender : AsyncFilter
    {
        readonly static ILog _log = LogManager.GetLogger(typeof(Sender));
        readonly string _name;        

        public string Name
        {
            get { return _name; }
        } 

        [DebuggerStepThrough]
        protected Sender(
            string name)
            : base(AsyncFilter.NoChildren)
        {
            _name = Safe.NonNull(name, "name");
        }
        
        public static IAsyncFilter FromXml(XElement e, IAsyncFilter[] children)
        {
            _log.Debug("Deserializing");
            Safe.NonNull(e, "e");
            Safe.Validate(e, () => "Sender" == e.Name.LocalName, "Not a Sender element");
            return new Sender(e.AttributeValue("name"));
        }

        public override void Process(IMessage message)
        {
            Debug.Write(".");
            base.Process(message);
        }
    }  
}